package com.zhang.two;

import com.zhang.base.AbstractList;

public class TwoLinkedList2<E> extends AbstractList<E> {
    private Node<E> first;
    private Node<E> last;

    private static class Node<E> {
        /**
         * 当前元素
         */
        E element;

        /**
         * 下一个节点
         */
        Node<E> next;

        /**
         * 上一个节点
         */
        Node<E> prev;

        Node(E element, Node next, Node prev) {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }

        @Override
        public String toString() {
            return element.toString();
        }
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {
            // 往最后插入
            Node<E> node = new Node<>(element, null, last);
            if (last == null) {
                last = node;
                first = node;
            } else {
                last.next = node;
                last = node;
            }
        } else {
            // 当index!=size 肯定又一个元素存在，找到需要插入的位置(也就是下一个节点)
            // 例如: size=2 index=1, size=1 index=0
            Node<E> next = node(index);
            // prev有可能为空
            Node<E> prev = next.prev;
            Node<E> node = new Node<>(element, next, prev);
            next.prev = node;
            if (prev == null) {
                // 插入为0的位置
                first = node;
            } else {
                prev.next = node;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = node(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;
        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
        }
        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    private Node<E> node(int index) {
        rangeCheck(index);
        Node<E> node;
        if (index < (size >> 1)) {
            node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            Node<E> prev = node.prev;
            Node<E> next = node.next;
            string.append(prev == null ? "null" : prev.element);
            string.append("_");
            string.append(node);
            string.append("_");
            string.append(next == null ? "null" : next.element);
            node = node.next;
        }
        string.append("]");
        return string.toString();
    }
}
